Grokking Microsoft Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Microsoft Easy Problems
Buddy Strings (easy)
Solution: Buddy Strings
Can Place Flowers (easy)
Solution: Can Place Flowers
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Maximum Number of Balloons (easy)
Solution: Maximum Number of Balloons
Binary Tree Path Sum (easy)
Solution: Binary Tree Path Sum
Pair with Target Sum (easy)
Solution: Pair with Target Sum
Buy Two Chocolates (easy)
Solution: Buy Two Chocolates
Root Equals Sum of Children (easy)
Solution: Root Equals Sum of Children
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Min Cost Climbing Stairs (easy)
Solution: Min Cost Climbing Stairs
Plus One (easy)
Solution: Plus One
Intersection of Two Arrays (easy)
Solution: Intersection of Two Arrays
Find the Index of the First Occurrence in a String (easy)
Solution: Find the Index of the First Occurrence in a String
Merge Two Sorted Lists (easy)
Solution: Merge Two Sorted Lists
Microsoft Medium Problems
Nth Digit (medium)
Solution: Nth Digit
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Find Original Array From Doubled Array (medium)
Solution: Find Original Array From Doubled Array
Reverse Words in a String II (medium)
Solution: Reverse Words in a String II
Longest Arithmetic Subsequence (medium)
Solution: Longest Arithmetic Subsequence
Matchsticks to Square (medium)
Solution: Matchsticks to Square
Score of Parentheses (medium)
Solution: Score of Parentheses
Maximum Length of a Concatenated String with Unique Characters (medium)
Solution: Maximum Length of a Concatenated String with Unique Characters
Inorder Successor in BST (medium)
Solution: Inorder Successor in BST
Distribute Coins in Binary Tree (medium)
Solution: Distribute Coins in Binary Tree
Spiral Matrix (medium)
Solution: Spiral Matrix
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Rotate Image (medium)
Solution: Rotate Image
Combinations (medium)
Solution: Combinations
Maximum Level Sum of a Binary Tree (medium)
Solution: Maximum Level Sum of a Binary Tree
Multiply Strings (medium)
Solution: Multiply Strings
Best Time to Buy and Sell Stock II (medium)
Solution: Best Time to Buy and Sell Stock II
Largest Palindromic Number (medium)
Solution: Largest Palindromic Number
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Next Permutation (medium)
Solution: Next Permutation
132 Pattern (medium)
Solution: 132 Pattern
Valid Sudoku (medium)
Solution: Valid Sudoku
Number of Islands (medium)
Solution: Number of Islands
Buildings With an Ocean View (medium)
Solution: Buildings With an Ocean View
Microsoft Hard Problems
Regular Expression Matching (hard)
Solution: Regular Expression Matching
First Missing Positive (hard)
Solution: First Missing Positive
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Sliding Window Maximum (hard)
Solution: Sliding Window Maximum
Count Unique Characters of All Substrings of a Given String (hard)
Solution: Count Unique Characters of All Substrings of a Given String
Word Ladder (hard)
Solution: Word Ladder
Word Ladder II (hard)
Solution: Word Ladder II
Number of Flowers in Full Bloom (hard)
Solution: Number of Flowers in Full Bloom
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Solution: First Missing Positive

Problem Statement

Given an unsorted array nums containing positive and negative integers, return the smallest missing positive integer value.

Note: You must implement algorithm in time and uses space.

Examples

  • Example 1:

    • Input: [5, 3, -1, 8, 4, 2]
    • Expected Output: 1
    • Justification: The first missing positive integer in the array is 1.
  • Example 2:

    • Input: [2, 5, -7, 3, 9, 4, 6, 1]
    • Expected Output: 7
    • Justification: The array contains all integers from 1 to 6, so the first missing positive integer is 7.
  • Example 3:

    • Input: [-4, -3, -2, -1]
    • Expected Output: 1
    • Justification: Since there are no positive integers, the first missing positive integer is 1.

Solution

To solve this problem, we'll employ an in-place algorithm to rearrange the elements such that each positive integer (i) is placed at the index (i-1), ignoring negative numbers and numbers larger than the array's length. This approach ensures that if a positive integer is missing, its corresponding index will not hold the correct value.

Our solution is effective because it allows us to identify the missing positive integer with a single pass through the array to rearrange the elements, followed by another pass to find the first index not holding its corresponding positive integer. This method avoids the need for extra space for another data structure, making it efficient in terms of both time and space complexity.

Step-by-step Algorithm

  • Initialize a variable n to store the length of the array.
  • First Pass: Iterate over the array.
    • For each element num at index i, while num is within the range [1, n] and the element at num-1 is not num, swap num with the element at num-1.
  • Second Pass: Iterate over the array again.
    • If the element at index i is not i+1, return i+1.
  • If all positions contain the correct integers, return n+1 to indicate that the first missing positive is just after the last index.

Algorithm Walkthrough

Let's consider the input: [2, 5, -7, 3, 9, 4, 6, 1]

Step 1: Start with the first element (2)

  • Element is 2; it should be at index 1 (since we are aiming for (i) at (i-1) index, considering 1-based indexing for the target positions).
  • Swap 2 with the element at its target index (1), which is 5.
  • Array after swap: [5, 2, -7, 3, 9, 4, 6, 1]

Step 2: Look at the element now at index 0 (5)

  • Element is 5; it should be at index 4.
  • Swap 5 with the element at index 4 (9).
  • Array after swap: [9, 2, -7, 3, 5, 4, 6, 1]

Step 3: Process the element now at index 0 (9)

  • Element is 9; it's out of range for our placement (since there is no index 8), so we leave it as is.

Step 4: Move to the second element (2)

  • Element is 2; it should be at index 1.
  • Swap 2 with the element at index 1 (itself in this case, no action needed).
  • Array state remains: [9, 2, -7, 3, 5, 4, 6, 1]

Step 5: Move to the third element (-7)

  • Element is -7; negative numbers are ignored.

Step 6: Move to the fourth element (3)

  • Element is 3; it should be at index 2.
  • Swap 3 with the element at index 2 (-7).
  • Array after swap: [9, 2, 3, -7, 5, 4, 6, 1]

Step 7: Move to the element now at index 3 (-7)

  • Element is -7; negative numbers are ignored.

Step 8: Move to the fifth element (5)

  • Element is 5; it should be at index 4.
  • It's already in the correct position.

Step 9: Move to the sixth element (4)

  • Element is 4; it should be at index 3.
  • Swap 4 with the element at index 3 (-7).
  • Array after swap: [9, 2, 3, 4, 5, -7, 6, 1]

Step 10: Move to the seventh element (6)

  • Element is 6; it should be at index 5.
  • Swap 6 with the element at index 5 (-7).
  • Array after swap: [9, 2, 3, 4, 5, 6, -7, 1]

Step 11: Move to the third element (-7)

  • Element is -7; negative numbers are ignored.

Step 12: Move to the eighth element (1)

  • Element is 1; it should be at index 0.
  • Swap 1 with the element at index 0 (9).
  • Array after swap: [1, 2, 3, 4, 5, 6, -7, 9]

Final Array State

[1, 2, 3, 4, 5, 6, -7, 9]

Finding the First Missing Positive

  • Start from the beginning of the array, checking if the element matches its index + 1.
  • All elements from 1 to 6 are correctly placed.
  • The first missing positive integer, given our repositioning, is at index 6 (considering 0-based indexing), where we expect to find 7, but we find -7.

Result

After rearranging the array and checking for the first non-matching index, the first missing positive integer for the array [2, 5, -7, 3, 9, 4, 6, 1] is determined to be 7.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

  • : The algorithm passes through the array a constant number of times (specifically, two passes). Each pass is linear in terms of the number of elements in the array. Therefore, the total operations are proportional to the number of elements, (n).

Space Complexity

  • : The space complexity is constant because the algorithm rearranges the elements in place without using any additional data structures that grow with the size of the input array.
First Missing Positive (hard)
Largest Rectangle in Histogram (hard)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Final Array State

Code

Complexity Analysis

Time Complexity

Space Complexity